package feng.demo.service;

import feng.demo.dao.CommodityDao;
import feng.demo.dao.OrderDao;
import feng.demo.dao.OrditemDao;
import feng.demo.dao.UserDao;
import feng.demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@CacheConfig(cacheNames = {"order"})
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;
    @Autowired
    CommodityService commodityService;
    @Autowired
    OrditemService orditemService;
    @Override
    @Cacheable
    public List<Order> findAll() {
        List<Order> list = orderDao.findAll();
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Order> findOrder(Map<String, Object> map) {
        List<Order> order = orderDao.findOrder(map);
        return doadds(order);
    }

    @Override
    @CacheEvict(value = "order", allEntries = true)
    public int add(Order order) {
        return orderDao.add(order);
    }

    @Override
    @CacheEvict(value = "order", allEntries = true)
    public Integer adds(Orders orders) {
        return orderDao.adds(orders);
    }

    @Override
    @CacheEvict(value = "order", allEntries = true)
    public int del(Integer ordid) {
        return orderDao.del(ordid);
    }

    @Override
    @CacheEvict(value = "order", allEntries = true)
    public int update(Order order) {
        return orderDao.update(order);
    }

    @Override
    @Cacheable(key = "#p0")
    public Order findById(Integer ordid) {
        Order order = orderDao.findById(ordid);
        return doadd(order);
    }

    @Override
    @Cacheable
    public List<Orders> findAlls() {
        List<Orders> list = orderDao.findAlls();
        return doaddss(list);
    }

    @Override
    @Cacheable(key = "#p0")
    public Orders findByIds(Integer ordid) {
        Orders orders = orderDao.findByIds(ordid);
        User user = userDao.findByUid(orders.getUser().getUid());
        List<Orditem> orditems = orditemService.findByordid(ordid);
        orders.setUser(user);
        orders.setOrditems(orditems);
        return orders;
    }

    @Override
    @Cacheable
    public List<Orders> findOrders(Order order) {
        List<Orders> orders = orderDao.findOrders(order);
        return doaddss(orders);
    }

    @Override
    @Cacheable
    public List<Orders> findAllsLimit(Integer pagenum, Integer lines) {
        List<Orders> list = orderDao.findAllsLimit(pagenum, lines);

        return doaddss(list);
    }

    @Override
    @Cacheable
    public List<Orders> findByUidLimit(Integer uid, Integer pagenum, Integer lines) {
        List<Orders> list = orderDao.findByUidLimit(uid, pagenum, lines);

        return doaddss(list);
    }

    @Override
    @Cacheable
    public List<Orders> findByCusLimit(Integer uid, Integer pagenum, Integer lines) {
        List<Orders> list = orderDao.findByCusLimit(uid, pagenum, lines);
        return doaddss(list);
    }

    @Override
    public int findCountAll() {
        return orderDao.findCountAll();
    }

    @Override
    public int findCountByUid(Integer uid) {
        return orderDao.findCountByUid(uid);
    }

    private List<Orders> doaddss(List<Orders> list){
        for (Orders o:list) {
            User user = userDao.findByUid(o.getUser().getUid());
            o.setUser(user);
            List<Orditem> orditems = orditemService.findByordid(o.getOrdid());
            o.setOrditems(orditems);
        }
        return list;
    }
    private List<Order> doadds(List<Order>list){
        for (Order o:list) {
            User user = userDao.findByUid(o.getUser().getUid());
            o.setUser(user);
        }
        return list;

    }
    private Order doadd(Order o){
        User user = userDao.findByUid(o.getUser().getUid());
        o.setUser(user);
        return o;
    }
}
